package com.enation.app.shop.component.goodscore.plugin.supplier;

import com.enation.app.shop.ShopApp;
import com.enation.app.shop.core.goods.plugin.AbstractGoodsPlugin;
import com.enation.app.shop.core.goods.plugin.IGoodsTabShowEvent;
import com.enation.app.shop.front.policyInsurance.entity.Supplier;
import com.enation.app.shop.front.policyInsurance.service.ISupplierMapper;
import com.enation.eop.processor.core.freemarker.FreeMarkerPaser;
import com.enation.eop.sdk.context.EopSetting;
import com.enation.framework.database.IDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Component
public class SupplierPlugin extends AbstractGoodsPlugin implements IGoodsTabShowEvent {
    @Autowired
    private ISupplierMapper iSupplierMapper;
    @Autowired
    private IDaoSupport iDaoSupport;
    @Override
    public String getAddHtml(HttpServletRequest request) {
        List<Supplier> supplierList=iSupplierMapper.getAll();
        FreeMarkerPaser freeMarkerPaser = FreeMarkerPaser.getInstance();
        freeMarkerPaser.setPageName("supplierList_select");
        freeMarkerPaser.putData("supplierList",supplierList);

        freeMarkerPaser.putData("optype",request.getParameter("optype")); //操作类型
        if("b2b2c".equals(EopSetting.PRODUCT)){
            freeMarkerPaser.putData("self_store","yes");
            freeMarkerPaser.putData("store_id", ShopApp.self_storeid);
        }
        return freeMarkerPaser.proessPageContent();
    }

    @Override
    public String getEditHtml(Map goods, HttpServletRequest request) {
        FreeMarkerPaser freeMarkerPaser = FreeMarkerPaser.getInstance();
        freeMarkerPaser.putData("goodsView",goods);
        freeMarkerPaser.putData("optype",request.getParameter("optype"));
        freeMarkerPaser.setPageName("goods_edit");
        if("b2b2c".equals(EopSetting.PRODUCT)){
            freeMarkerPaser.putData("self_store","yes");
            freeMarkerPaser.putData("store_id",ShopApp.self_storeid);
        }
        return freeMarkerPaser.proessPageContent();
    }

    @Override
    public void onAfterGoodsAdd(Map goods, HttpServletRequest request) throws RuntimeException {

    }

    @Override
    public void onAfterGoodsEdit(Map goods, HttpServletRequest request) {

    }

    @Override
    public void onBeforeGoodsAdd(Map goods, HttpServletRequest request) {

    }

    @Override
    public void onBeforeGoodsEdit(Map goods, HttpServletRequest request) {
        this.iDaoSupport.execute("update es_cart set is_change = 1 where goods_id = ?", goods.get("goods_id"));
    }

    @Override
    public String getTabName(Integer typeid) {
        return "供应商";
    }

    @Override
    public int getOrder() {
        return 12;
    }
}
